package com.hst.capacity.application.jpush.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.hst.capacity.application.jpush.model.reponse.JPushResult;
import com.hst.capacity.application.jpush.service.JPushService;
import com.hst.capacity.application.jpush.util.JPushUtil;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.model.entity.driver.DriverEntity;
import com.hst.capacity.domain.model.response.order.OrderVO;
import com.hst.capacity.infra.persistence.jpa.driver.DriverRepository;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JPushServiceImpl implements JPushService {

    @Autowired
    private DriverRepository driverRepository;

    @Override
    public void sendNewOrderMessage(OrderVO order) {

        //定义消息内容
        JSONObject jsonObject=new JSONObject();
        //1：实时单 2：预约单
        jsonObject.put("type", order.getOrderType() + 1);
        jsonObject.put("idOrder", order.getIdOrder());
        jsonObject.put("status:", order.getStatus());
        jsonObject.put("orderSource", order.getOrderSource());
        if (Constants.OrderType.REALTIME == order.getOrderType()) {
            jsonObject.put("message", "您收到一个实时的订单,请打开APP，准时到达服务地点。");
        } else {
            String message = String.format("您有一个从{}到{}的预约单,请点击APP查看。",order.getStartAddress(),order.getEndAddress());
            jsonObject.put("message", message);
        }

        if (StringUtil.isNotBlank(order.getIdDriver())) {
            DriverEntity driver = driverRepository.findByIdDriver(order.getIdDriver());
            if (StringUtil.isNotBlank(driver.getIdJpush())) {
                JPushResult result = JPushUtil.sendMessage(driver.getIdJpush(), jsonObject.toJSONString(), "message");
            }
        }

    }

    @Override
    public void sendOrderCancelMessage(OrderVO order) {

        //定义消息内容
        JSONObject jsonObject=new JSONObject();
        //1：实时单 2：预约单
        jsonObject.put("type", order.getOrderType() + 1);
        jsonObject.put("idOrder", order.getIdOrder());
        jsonObject.put("status:", order.getStatus());
        jsonObject.put("orderSource", order.getOrderSource());
        String message = String.format("从{}到{}的订单已取消，请注意刷新订单。",order.getStartAddress(),order.getEndAddress());
        jsonObject.put("message", message);

        if (StringUtil.isNotBlank(order.getIdDriver())) {
            DriverEntity driver = driverRepository.findByIdDriver(order.getIdDriver());
            if (StringUtil.isNotBlank(driver.getIdJpush())) {
                JPushResult result = JPushUtil.sendMessage(driver.getIdJpush(), jsonObject.toJSONString(), "message");
            }
        }
    }

    @Override
    public void sendOrderCompleteMessage(OrderVO order) {

        //定义消息内容
        JSONObject jsonObject=new JSONObject();
        //1：实时单 2：预约单
        jsonObject.put("type", order.getOrderType() + 1);
        jsonObject.put("idOrder", order.getIdOrder());
        jsonObject.put("status:", order.getStatus());
        jsonObject.put("orderSource", order.getOrderSource());
        String message = String.format("从{}到{}的订单已完成，感谢您的服务。",order.getStartAddress(),order.getEndAddress());
        jsonObject.put("message", message);

        if (StringUtil.isNotBlank(order.getIdDriver())) {
            DriverEntity driver = driverRepository.findByIdDriver(order.getIdDriver());
            if (StringUtil.isNotBlank(driver.getIdJpush())) {
                JPushResult result = JPushUtil.sendMessage(driver.getIdJpush(), jsonObject.toJSONString(), "message");
            }
        }

    }

}
